package com.yuwenchao.superafflatus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuwenchao.superafflatus.pojo.entity.SuperSalary;
import com.yuwenchao.superafflatus.pojo.vo.SuperSalaryVo;
import com.yuwenchao.superafflatus.service.SuperSalaryService;
import com.yuwenchao.superafflatus.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChaooGG
 * @since 2021-05-31
 */


@Slf4j
@RestController
@RequestMapping("/superSalary")
@AllArgsConstructor
@Api(tags = "薪资详情展示")
public class SuperSalaryController {

    @Autowired
    SuperSalaryService superSalaryService;


    @Autowired
    RedisTemplate<Object,Object> redisTemplate;



    @ApiOperation("查询薪资统计信息")
    @GetMapping("getSalaryCount")
    // @ApiParam(value = "公司编号1,2", required = false)  参数
    public R getSalaryCount(String company){
        SuperSalaryVo salaryVo  =superSalaryService.getSalaryCount(company);
        return R.ok().data("salaryVo",salaryVo);
    }

    @ApiOperation("按年统计薪资信息展示")
    @GetMapping("getSalaryInfoByYear")
    public R getSalaryInfoByYear(@RequestParam(value = "year",required = false) String year){
        /*String start="";
        String end="";
        if(AfflatusConstant.YEAR2019.equals(year)){
           start ="201901";
            end ="201912";
        }else if(AfflatusConstant.YEAR2020.equals(year)){
            start ="202001";
            end ="202012";
        }
        else if(AfflatusConstant.YEAR2021.equals(year)){
            start ="202101";
             end ="202112";
        }else{
            return R.error().message("数据不存在");
        }*/
        if (year==""&& year.isEmpty()){
            SuperSalaryVo salaryVoYear =superSalaryService.getSalaryCountBySumYear();
            return R.ok().data("salaryVoYear",salaryVoYear);
        }

        //SuperSalaryVo salaryVoYear  =superSalaryService.getSalaryCountByYear(start,end);
        SuperSalaryVo salaryVoYear  =superSalaryService.getSalaryByYear(year);
        return R.ok().data("salaryVoYear",salaryVoYear);
    }

    @ApiOperation("最近月份薪资信息展示")
    @GetMapping("getSalaryInfo")
    public R getSalaryInfo(@RequestParam(value = "statDate",required = false) String statDate,
                           @RequestParam(value = "endDate",required = false) String endDate,
                           @RequestParam(value = "company",required = false) String company,
                           @RequestParam(value = "total",defaultValue = "5") Integer total){

        //带查询的数据不建议从redis数据库中取值
       // Object salaryList = redisTemplate.opsForList().leftPop("salaryList");

            if(statDate!=""&&endDate!="") {
                statDate = statDate.replaceAll("-", "").substring(0,6);
                endDate = endDate.replaceAll("-", "").substring(0,6);
                int staryear = Integer.parseInt(statDate.substring(0,4));
                int starmonth = Integer.parseInt(statDate.substring(4,6));
                int endyear = Integer.parseInt(endDate.substring(0,4));
                int endmonth = Integer.parseInt(endDate.substring(4,6));
                log.info("截取到的起始数据年："+staryear+"月："+starmonth);
                log.info("截取到的结束数据年："+endyear+"月："+endmonth);
                if(endyear-staryear==0){
                    total =1;
                }
                if(endyear>=staryear){
                    total= (endyear-staryear)*12+(endmonth-starmonth)+1;
                    log.info("相差的月份："+total);
                }
            }

            List<SuperSalary>  list =superSalaryService.getSalaryInfo(statDate,endDate,company,total);

            return R.ok().data("list",list);

    }


    @ApiOperation("新增薪资信息")
    @PostMapping("saveSalaryInfo")
    public R saveSalaryInfo(@RequestBody SuperSalary superSalary){
        boolean save = false;
        try {
            save = superSalaryService.save(superSalary);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加数据异常！");
        }
        if(save){
            return R.ok().message("添加数据成功！");
        }else{
            return R.error().message("添加数据失败！");
        }

    }


    @ApiOperation("根据月份删除")
    @DeleteMapping("deleteSalaryInfo/{month}")
    public R deleteSalaryInfoByMonth(@PathVariable String month){
        boolean flag =superSalaryService.deleteSalaryInfoByMonth(month);
        if(flag){
            return R.ok().message("删除数据成功！");
        }else {
            return R.error().message("删除数据失败！");
        }

    }


    @ApiOperation("根据月份修改")
    @PutMapping("editSalaryInfo/{month}")
    public R editSalaryInfoByMonth(@PathVariable String month,@RequestBody SuperSalary superSalary){
        QueryWrapper<SuperSalary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("month",month);
        boolean flag =superSalaryService.update(superSalary,queryWrapper);
        if(flag){
            return R.ok().message("修改数据成功!");
        }else {
            return R.error().message("修改数据失败!");
        }

    }
}

